package cn.sskxyz.security.authorization.web.controller;

import cn.sskxyz.mybatis.mode.Page;
import cn.sskxyz.security.authorization.model.Oauth2Client;
import cn.sskxyz.security.authorization.model.SecurityScope;
import cn.sskxyz.security.authorization.model.SecurityService;
import cn.sskxyz.security.authorization.service.Oauth2ClientService;
import cn.sskxyz.security.authorization.service.ProtectedResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SecurityServiceController {

    @Autowired
    private ProtectedResourceService resourceService;

    @Autowired
    private Oauth2ClientService clientService;

    /**
     * 添加受管理的服务
     * @param securityService
     */
    @PostMapping("/service")
    public void addService(SecurityService securityService) {
        resourceService.addService(securityService);
    }

    /**
     * 获取受管理的服务列表
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/service")
    public ResponseEntity<Page<List<SecurityService>>> getProtectedService(@RequestParam(required = false,defaultValue = "1") int currentPage,
                                                                           @RequestParam(required = false,defaultValue = "10") int pageSize) {
        Page<List<SecurityService>> result = resourceService.queryProtectedService(currentPage, pageSize);
        return ResponseEntity.ok(result);
    }

    /**
     * 修改受管理的服务信息
     * @param securityService
     * @return
     */
    @PutMapping("/service")
    public ResponseEntity changeProtectedService(@RequestBody SecurityService securityService) {
        resourceService.changeProtectedService(securityService);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询访问某服务需要配置的权限
     * @param serviceId
     * @return
     */
    @GetMapping("/service/{serviceId}/scope")
    public ResponseEntity<List<SecurityScope>> getServiceScopes(@PathVariable int serviceId){
        List<SecurityScope> scopes = resourceService.queryServiceScope(serviceId);
        return ResponseEntity.ok(scopes);
    }


    /**
     * 设置访问某服务所需要的scope
     * @param serviceId
     * @param scopeId
     * @param status
     * @return
     */
    @PostMapping("/service/scope/{serviceId}")
    public ResponseEntity associateServiceScope(@PathVariable int serviceId, int scopeId, int status) {
        resourceService.associateServiceScope(serviceId, scopeId, status);
        return ResponseEntity.ok().build();
    }

    /**
     * 设置客户端可以访问的服务
     * @param serviceId
     * @param clientId
     * @param status
     * @return
     */
    @PostMapping("/service/client/{serviceId}")
    public ResponseEntity associateServiceClient(@PathVariable int serviceId, String clientId, int status) {
        Oauth2Client client = clientService.loadClientByClientId(clientId);
        if (client == null) {
            return ResponseEntity.noContent().build();
        }
        resourceService.associateServiceClient(serviceId, clientId, status);
        return ResponseEntity.ok().build();
    }
}
